package com.jingdianjichi.oss.adapter;

import com.jingdianjichi.oss.adapter.adapterImpl.StorageAdapter;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 他和我们的storageService做交互去,这样 controller层,在一次的和Storage去做一次解耦来的
 *      /这个就是中间层来的
 */
@Service
public class FileService_adapter {

    private final StorageAdapter storageAdapter;

    //使用构造函数去初始化,这里是使用Spring自动注入的,你也可以通过上面的属性进行注入
    public FileService_adapter(StorageAdapter storageAdapter) {
        this.storageAdapter = storageAdapter;
    }


    /**
     * 列出所有的桶
     */
    public List<String> getAllBucket(){
        return storageAdapter.getAllBucket();
    }

    /**
     * 上传文件,对象名最后一个就是
     */
    public String uploadFile(MultipartFile uploadFile, String bucket, String objectName) throws Exception{
        /*
            storageAdapter.uploadFile(uploadFile,bucket,objectName);
            return storageAdapter.getURL(bucket,objectName);

            //这样返回的地址太麻烦了,我们更新成下面的
         */

        storageAdapter.uploadFile(uploadFile,bucket,objectName);

        objectName = objectName + "/" + uploadFile.getOriginalFilename();

        String url = storageAdapter.getURL(bucket, objectName);

        return url;
    };

}
